package com.ju.demo.controller;

import com.ju.demo.bo.ExchangeTeamInfo;
import com.ju.demo.constant.ResultCodeConstant;
import com.ju.demo.dto.ExchangeTeamDTO;
import com.ju.demo.entity.ExchangeTeam;
import com.ju.demo.service.ExchangeTeamService;
import com.ju.demo.service.ExchangeTeamService2;
import com.ju.demo.util.LogUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("controller-交换班信息处理控制器")
@Controller
@RequestMapping("/exchangeTeam")
public class ExchangeTeamController {

    @Autowired
    private ExchangeTeamService exchangeTeamService;


    @ApiOperation("查询交换班列表信息BY日期")
    @GetMapping("/team1/{selectionDate}")
    @ResponseBody
    public String selectByDate(@PathVariable("selectionDate")Integer selectionDate){

        LogUtil.write("准备查询日期：date" + selectionDate);

        if (ObjectUtils.isEmpty(selectionDate)) {
            return "false";
        }
        String s = selectionDate.toString();
        List<ExchangeTeam> teamList = exchangeTeamService.findTeamsByTime(s);

        for (ExchangeTeam team : teamList) {
            LogUtil.write(team.toString());
        }

        if (teamList == null){
            LogUtil.write("返回值为空！");
            return "false";
        }

        return "true";
    }

    @ApiOperation("查询具体交换班信息BY序号&日期")
    @GetMapping("/team1/{id}/{date}")
    @ResponseBody
    public String selectById(@PathVariable("id")Integer id,
                           @PathVariable("date")String date){

        LogUtil.write("准备查询的id:" + id);
        LogUtil.write("准备查询的日期date:" + date);

        if (ObjectUtils.isEmpty(id)) {
            return "false";
        }
        ExchangeTeamInfo team = exchangeTeamService.findTeamByID(id,date);
        if (team==null){
            LogUtil.write("返回值为空！");
            return "false";
        }
        return "true";
    }

    @ApiOperation("删除交换班信息BY序号")
    @DeleteMapping("/team/{id}")
    @ResponseBody
    public String deleteExchangeTeam(@PathVariable("id")Integer id){

        LogUtil.write("准备删除交换班id:" + id);

        if (ObjectUtils.isEmpty(id)) {
            return "false";
        }
        ResultCodeConstant code = exchangeTeamService.deleteExchangeTeam(id);

        LogUtil.write("是否完成删除：" + code);
        return "true";
    }

    @ApiOperation("更新交换班信息BY序号&信息")
    @PutMapping("/team/{id}")
    @ResponseBody
    public String updateExchangeTeam(@PathVariable("id") Integer id,
                                     @RequestBody  ExchangeTeamInfo info){

        LogUtil.write("准备修改的数据id:" +id );
        LogUtil.write("准备修改的数据info:" + info);

        if ((id == null) && (info ==null) ) {
            return "false";
        }
        ResultCodeConstant code = exchangeTeamService.updateExchangeTeam(id, info);
        LogUtil.write("是否完成修改：" + code);
        return "true";
    }

    @ApiOperation("添加交换班信息BY新信息")
    @PostMapping("/team/")
    @ResponseBody
    public String addExchangeTeam(@RequestBody  ExchangeTeamInfo info){

        LogUtil.write("准备添加交换班信息：info" + info);

        if (ObjectUtils.isEmpty(info)){
            return "false";
        }
        ResultCodeConstant code = exchangeTeamService.addExchangeTeam(info);
        LogUtil.write("是否完成添加：" + code);
        return "true";
    }



}
